package com.mate.cloud.xml.dtd;

import com.mate.cloud.xml.AbstractTestCase;
import org.dom4j.dtd.AttributeDecl;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * 测试 {@link AttributeDecl}功能。测试每个属性访问方法和序列化机制。正确的解析由以下测试
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2024/6/3 14:40
 * @updateUser:     MI
 * @updateTime:     2024/6/3 14:40
 * @updateRemark:   修改内容
 * @version:        v1.0
 */
@SpringBootTest
public class AttributeDeclTests extends AbstractTestCase {


    // Test case(s)
    // -------------------------------------------------------------------------

    /**
     * Test
     *
     * <pre>
     *
     *  &lt;!ATTLIST foo bar ID #IMPLIED&gt;
     *
     * </pre>.
     */
    @Test
    public void testIdImpliedNone() {
        MyTestAttributeDecl decl = new MyTestAttributeDecl("foo", // elementName
                "bar", // attributeName
                "ID", // type
                "#IMPLIED", // valueDefault
                null, // value
                "<!ATTLIST foo bar ID #IMPLIED>");
        assertSameAttributeDecl(decl, new AttributeDecl("foo", "bar", "ID",
                "#IMPLIED", null));
    }

    /**
     * Test
     *
     * <pre>
     *
     *  &lt;!ATTLIST foo bar CDATA #FIXED \&quot;goo\&quot;&gt;
     *
     * </pre>.
     */
    @Test
    public void testCDataFixedValue() {
        MyTestAttributeDecl decl = new MyTestAttributeDecl("foo", // elementName
                "bar", // attributeName
                "CDATA", // type
                "#FIXED", // valueDefault
                "goo", // value
                "<!ATTLIST foo bar CDATA #FIXED \"goo\">");
        assertSameAttributeDecl(decl, new AttributeDecl("foo", "bar", "CDATA",
                "#FIXED", "goo"));
    }

    /**
     * Test
     *
     * <pre>
     *
     *  &lt;!ATTLIST foo bar CDATA &quot;goo&quot;&gt;
     *
     * </pre>.
     */
    @Test
    public void testCDataNoneValue() {
        MyTestAttributeDecl decl = new MyTestAttributeDecl("foo", // elementName
                "bar", // attributeName
                "CDATA", // type
                null, // valueDefault
                "goo", // value
                "<!ATTLIST foo bar CDATA \"goo\">");
        assertSameAttributeDecl(decl, new AttributeDecl("foo", "bar", "CDATA",
                null, "goo"));
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    protected void assertSameAttributeDecl(MyTestAttributeDecl expected,
                                           AttributeDecl actual) {
        assertEquals("elementName is correct", expected.getElementName(),
                actual.getElementName());

        assertEquals("attributeName is correct", expected.getAttributeName(),
                actual.getAttributeName());

        assertEquals("type is correct", expected.getType(), actual.getType());

        assertEquals("valueDefault is correct", expected.getValueDefault(),
                actual.getValueDefault());

        assertEquals("toString() is correct", expected.getText(), actual
                .toString());
    }

    /**
     * Helper is useful since we are trying to exhaustively test the ATTLIST
     * variations and their correct serialization.
     */
    protected static class MyTestAttributeDecl {
        private String elName;

        private String attName;

        private String declType;

        private String defaultValue;

        private String declValue;

        private String txt;

        /**
         * DOCUMENT ME!
         *
         * @param elementName
         *            The name of the element whose attribute is being
         *            described.
         * @param attributeName
         *            The name of the attribute.
         * @param type
         *            The type of the declared attribute, e.g., CDATA, ID,
         *            IDREF, IDREFS, ENTITY, ENTITIES, NMTOKEN, NKTOKENS
         * @param valueDefault
         *            The type of default that applies for this attribute
         *            declaration, e.g., #REQUIRED, #IMPLIED, #FIXED (in which
         *            case the <i>value </i> MUST be non- <code>null</code>
         *            and specifies the fixed value for the attribute, or
         *            <code>null</code> if no valueDefault was specified in
         *            the attribute declaration (in which case the <i>value </i>
         *            MUST be non- <code>null</code> and specifies the default
         *            value for the attribute).
         * @param value
         *            The value of the attribute assigned in the attribute
         *            declaration -or- <code>null</code> if no value was
         *            provided in the attribute declaration. The value MUST be
         *            <code>null</code> unless the <i>valueDefault </i> is
         *            either "#FIXED" or <code>null</code>.
         * @param text
         *            The text representation of the attribute declaration,
         *            e.g., <code>&lt;!ATTLIST foo id ID #IMPLIED&gt;</code>.
         *
         * @todo The constructor and properties in {@link AttributeDecl}should
         *       have some similar javadoc so that people more easily understand
         *       the interaction and difference between the <i>valueDefault </i>
         *       and <i>value </i> properties. The constructor SHOULD be clear
         *       about whether and when the <code>valueDefault</code> and
         *       <code>value</code> properties MUST be <code>null</code>.
         */
        public MyTestAttributeDecl(String elementName, String attributeName,
                                   String type, String valueDefault, String value, String text) {
            elName = elementName;

            attName = attributeName;

            declType = type;

            defaultValue = valueDefault;

            declValue = value;

            txt = text;
        }

        public String getElementName() {
            return elName;
        }

        public String getAttributeName() {
            return attName;
        }

        public String getType() {
            return declType;
        }

        public String getValueDefault() {
            return defaultValue;
        }

        public String getValue() {
            return declValue;
        }

        public String getText() {
            return txt;
        }
    } // Class TestAttributeDecl
}
